package com.quzi.kettle.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.text.format.DateFormat;
import android.util.Log;

public class DateUtils {

	
	public static int diffMinute(int ampm, int hour, int minute){
		int minutes = 0;
		
		Date date = new Date(System.currentTimeMillis());
		
		if(ampm == 1){
			hour +=12;
		}
		
		int targetMinutes = hour*60+minute;
		int currMinutes = date.getHours()*60+date.getMinutes();
		if(targetMinutes >= currMinutes){
			minutes = targetMinutes - currMinutes;
		}else{
			minutes = targetMinutes+24*60 - currMinutes;
		}
		
		return minutes;
	}
	
	
	public static int getCurrHour(){
		Date date = new Date(System.currentTimeMillis());
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, 1);
		return cal.get(Calendar.HOUR_OF_DAY);
	}
	
	public static int getCurrMinute(){
		Date date = new Date(System.currentTimeMillis());
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, 1);
		return cal.get(Calendar.MINUTE);
	}
	
	public static int getCurrTimeInMinuts(){
		Date date = new Date(System.currentTimeMillis());
		int hour = date.getHours();
		int minute = date.getMinutes();
		int result = hour*60+minute;
		return result;
	}
	
	/**
	 * 获取时间差的 时
	 * @param inMinutes
	 * @return
	 */
	public static int diffTimeHour(int inMinutes){
		int diffTime = inMinutes - getCurrTimeInMinuts();
		if(diffTime < 0){
			diffTime = 0;
		}
		int result = diffTime/60;
		return result;
	}
	
	/**
	 * 获取时间差的 分
	 * @param inMinutes
	 * @return
	 */
	public static int diffTimeMinute(int inMinutes){
		int diffTime = inMinutes - getCurrTimeInMinuts();
		if(diffTime < 0){
			diffTime = 0;
		}
		int result = diffTime%60 - Calendar.getInstance().get(Calendar.MINUTE);
		if(result < 0){
			result = 0;
		}
		
		return result;
	}
	
	/**
	 * 返回预约距离现在的时间
	 * @param setTime	预约时间
	 * @return
	 */
	public static String diffTime(int inMinutes){
		int diffTime = Math.abs(inMinutes - getCurrTimeInMinuts());
		
		Log.d("NumberPicker","时间差 = "+diffTime+" in minute"+inMinutes+" "+getCurrTimeInMinuts());
		if(diffTime < 0){
			diffTime = 0;
		}
		int hour = (int) (diffTime/60);
		int minute = (int) (diffTime%60);
		StringBuilder sb = new StringBuilder();
		if(hour < 10){
			sb.append("0"+hour);
		}else{
			sb.append(hour);
		}
		sb.append("小时");
		if(minute < 10){
			sb.append("0"+minute);
		}else{
			sb.append(minute);
		}
		sb.append("分钟");
		return sb.toString();
	}
	
	/**
	 * 获取图表的X轴，日数据
	 * @param date
	 * @return
	 */
	public static String[] getDaysOfTargetMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int days = cal.get(Calendar.DAY_OF_MONTH);
		String[] results = new String[days];
		for(int i=0;i<results.length;i++){
			results[i] = (1+i)+"日";
		}
		return results;
	}
	
	/**
	 * 获取前几天的日期
	 * @param num
	 * @return
	 */
	public static String[] getWeekDay(int num){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date(System.currentTimeMillis()));
		String[] result = new String[num];
		for(int i=0;i<num;i++){
			result[i] = c.get(Calendar.MONTH)+1 + "月" + c.get(Calendar.DAY_OF_MONTH) + "日";
			c.add(Calendar.DAY_OF_MONTH, -1);
		}
		return result;
	}
	
	public static String getTodayDate(){
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	
	
}
